﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Adex;
using MediaMonitor.Common;

namespace MediaMonitor.Admin
{
    public partial class Register : Adex.BasePage
    {




        protected void Page_Load(object sender, System.EventArgs e)
        {

        }




        protected void btnSave_Click(object sender, System.EventArgs e)
        {
            if (!Page.IsValid)
            {
                return;
            }

            SiteUserController usrCont = new SiteUserController();

            string msg = "";


            //check email duplication
            if (AppUser.GetUserByEmail(this.tbEmail.Text.Trim()).SiteUserID > 0)
            {
                //AjaxFuncs.MsgBox(this.Page, "This email already used by another user. try with some other email.");
                //return;
            }

            //check screen name
            if (AppUser.GetUserByUserName(this.tbScreenName.Text.Trim()).SiteUserID > 0)
            {
                AjaxFuncs.MsgBox(this.Page, "User with this screen name already exists. Write some different screen name");
                this.lblMsg.Text = "User with this screen name already exists. Write some different screen name";
                return;
            }

            //using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Suppress, TimeSpan.FromMinutes(10)))
            //{
            //    using (SubSonic.SharedDbConnectionScope con = new SubSonic.SharedDbConnectionScope(System.Configuration.ConfigurationManager.ConnectionStrings["DBAdex"].ToString()))
            //    {

            try
            {
                System.Web.Security.MembershipCreateStatus status = new System.Web.Security.MembershipCreateStatus();
                System.Web.Security.MembershipUser memUsr = System.Web.Security.Membership.CreateUser(this.tbScreenName.Text.Trim(), this.tbPassword.Text.Trim(), this.tbEmail.Text.Trim(), "Q", "a", true, out status);
                System.Web.Security.Roles.AddUserToRole(memUsr.UserName, lstRole.SelectedItem.Text);

                switch (status)
                {
                    case System.Web.Security.MembershipCreateStatus.Success:
                        //add user to membership
                        Guid userGuid = (Guid)memUsr.ProviderUserKey;
                        //add this user to user's role

                        //Roles.AddUserToRole(memUsr.UserName, "user")

                        //'add this user to tblSiteUser
                        SiteUser usr = new SiteUser();



                        usr.UserID = (Guid)memUsr.ProviderUserKey;
                        usr.Email = memUsr.Email;
                        usr.UserName = memUsr.UserName;


                        //Screen Name and Time Zone ID
                        usr.ScreenName = this.tbScreenName.Text.Trim();

                        string unlockCode = System.Guid.NewGuid().ToString().Replace("-", "").Substring(0, 6);
                        usr.UnlockCode = unlockCode;
                        usr.Active = true;


                        //generate promotional code
                        string pCode = "";
                        //pCode = DateTime.Now.ToShortDateString.ToString.Trim.Replace("/", "")
                        pCode = DateTime.Now.Date.Day.ToString() + DateTime.Now.Date.Month.ToString() + DateTime.Now.Date.Year.ToString().Substring(2, 2);

                        pCode += DateTime.Now.Hour.ToString();
                        pCode += DateTime.Now.Minute.ToString();
                        pCode += DateTime.Now.Second.ToString();

                        usr.PromotionCode = pCode;


                        int uid = 0;

                        uid = AppUser.Register(usr);

                        if (uid <= 0)
                        {
                            msg = "<b>Error!!! User not saved.. Contact Admin for this problem</b>";
                            System.Web.Security.Membership.DeleteUser(memUsr.UserName);
                        }

                        break;



                    case System.Web.Security.MembershipCreateStatus.DuplicateEmail:
                    case System.Web.Security.MembershipCreateStatus.DuplicateUserName:
                        msg = "<b>User with this Username already exists.</b><br /> Please choose some other email for your new account.";
                        break;
                    case System.Web.Security.MembershipCreateStatus.InvalidPassword:
                        msg = "<b>Wrong password format</b>";
                        break;
                    case System.Web.Security.MembershipCreateStatus.InvalidUserName:
                        msg = "<b>Invalid charachters found in username</b>";
                        break;
                    default:
                        msg = status.ToString();
                        break;
                }


                //ts.Complete();
                msg = "New User saved successfully.";
            }
            catch (Exception ex)
            {
                msg = msg + ". " + ex.Message;
            }
            finally
            {
                //ts.Dispose();
            }


            //    }

            //}


            this.lblMsg.Text = msg;
            //AjaxFuncs.MsgBox(me.page, msg)    



        }




    }

}